package cn.wx.scholar.neo4j.repository;

import java.util.List;

import org.neo4j.ogm.model.Result;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
/*import org.springframework.data.neo4j.repository.GraphRepository;*/
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import cn.wx.scholar.neo4j.entity.Neo4jField;

@Repository
public interface FieldRepository extends Neo4jRepository<Neo4jField,Long> {
	
	@Query("match(f:field)-[r:child]->(f1:field) where f.name=~{name} return f1.fId as id,f1.name,'child' as type " + 
			"union " + 
			"match(f:field)<-[r:child]-(f1:field) where f.name=~{name} return f1.fId as id,f1.name,'parent' as type")
	Result getFields(@Param("name") String name);
	
	/**
	 * 根据专家id 获取 领域的值
	 */
	@Query("match(f:field)-[r:containse]->(e:expert) where ID(e)={expertId} and f.topLevel<>1 return distinct f")
	List<Neo4jField> getFieldsByExpertId(@Param("expertId") Long expertId);
	
	/**
	 * 统计一下领域的专家数
	 * @param fIds
	 * @return
	 */
	@Query("match(f:field)-[r:containse]->(e:expert) where f.fId in {fIds} and e.nIndex>=-1 and e.nIndex<101 return f.fId as fId,count(e) as ecount")
	Result getFieldsExpertCount(@Param("fIds") List<Long> fIds);
}
